﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using SuperQuiz.Model;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.IO;

namespace SuperQuiz
{
    public class Controller
    {
        private static Controller instance;
        public Quiz Quiz;

        public static Controller Instance
        {
            get
            {
                if (instance == null)
                    instance = new Controller();
                return instance;
            }
        }


        private void Reset()
        {
            IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();

            Quiz = new Quiz();
            Parser.Parser parser = new Parser.Parser("abc.txt");
            Quiz.Tests = new System.Collections.Generic.Dictionary<string, Test>();
            foreach (Test t in parser.Tests.Values)
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains(t.Name))
                {
                    t.LastPerfomance = ((int)IsolatedStorageSettings.ApplicationSettings[t.Name]);
                }
                else
                {
                    t.LastPerfomance = -1;
                }

                Quiz.Tests[t.Name] = t;
            }
        }


        public void Init()
        {
            using (IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myStore.FileExists("abc.txt") == false)
                {
                    Uri from = new Uri("Sample.txt", UriKind.RelativeOrAbsolute);
                    String local = "abc.txt";
                    CopyFromResourceToStorage(from, local);
                }
            }
            Reset();
        }

        private void CopyFromResourceToStorage(Uri uri, string local)
        {
            Stream stream = App.GetResourceStream(uri).Stream;
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            using (var fs = new IsolatedStorageFileStream(local, FileMode.Create, store))
            {
                int size = (int) stream.Length;

                byte[] mem = new byte[size];

                stream.Read(mem, 0, size);
                fs.Write(mem, 0, size);

                fs.Flush();
                fs.Close();
                stream.Close();
            }
        }

        internal void SaveData()
        {
            IsolatedStorageSettings.ApplicationSettings["Quiz"] = this.Quiz;
            IsolatedStorageSettings.ApplicationSettings.Save();
        }

        internal void LoadData()
        {
            Init();
            if (IsolatedStorageSettings.ApplicationSettings.Contains("Quiz"))
            {
                this.Quiz = (Quiz)IsolatedStorageSettings.ApplicationSettings["Quiz"];
            }
            else
            {
                Init();
            }
        }
    }
}
